package wt.hpad.activity.detail;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import wt.hpad.R;
import wt.hpad.model.bean.DishDetail;
import wt.hpad.utils.ActivityUtil;
import wt.hpad.utils.AppUtil;

import static wt.hpad.ConstantFields.CURRENT_TABLE;
import static wt.hpad.utils.LogUtil.logI;

/**
 * 桌台详情activity
 */
public class DetailActivity extends AppCompatActivity {

    @BindView(R.id.detail_content)
    FrameLayout detailContent;
    @BindView(R.id.tv_detail_title)
    TextView tvTitle;
    private ArrayList<DishDetail> datas;
    private DetailPresenter mDetailPresenter;
    private Unbinder bind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_act);

        initData();
        initUI();
    }

    private void initData() {
        datas = getIntent().getParcelableArrayListExtra("dishs");
    }

    private void initUI() {
        AppUtil.hideBarAndTransparentTime(this);
        AppUtil.hideBottomUIMenuAll(this);
        bind = ButterKnife.bind(this);
        initFragment();
        tvTitle.setText(CURRENT_TABLE);
    }

    private void initFragment() {
        DetailFragment detailFragment = new DetailFragment();
        ActivityUtil.addFragmentToActivity(getSupportFragmentManager(), detailFragment, R.id.detail_content);
        mDetailPresenter = new DetailPresenter(detailFragment, datas);
        detailFragment.setPresenter(mDetailPresenter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        logI("DetailActivity", "onDestroy |  ", "" + "");
        mDetailPresenter.unsubscribe();
        if (datas != null) {
            datas.clear();
            datas = null;
        }
        detailContent.destroyDrawingCache();
        detailContent.removeAllViews();
        bind.unbind();
        this.finish();
    }


    @OnClick(R.id.tv_detail_title)
    public void onViewClicked() {
        this.finish();
    }
}
